<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // typeof 准确判断基础类型 ,引用类型有缺陷
      // instanceof 准确判断复杂引用类型,判断不了基础类型
      // Object.prototype.toString.call() 推荐

      function getType(data) {
        let type = typeof data;
        if (type != "object") {
          return type;
        }
        return Object.prototype.toString
          .call(data)
          .replace(/^\[object (\S+)\]$/, "$1");
      }

      console.log(getType("hello"));
      console.log(getType(123));
      console.log(getType(true));
      console.log(getType(undefined));
      console.log(getType(null));
      console.log(getType([]));
      console.log(getType({}));
      console.log(getType(function () {}));
      console.log(getType(window));
      console.log(getType(document));
      console.log(getType(new Date()));
      console.log(getType(/123/g));
    </script>
  </body>
</html>
